package com.deliveryservicesystem.app.validation;

import com.jgoodies.binding.value.AbstractValueModel;
import com.jgoodies.common.base.Strings;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.Validator;

/**
 * File Name: BillingValidator.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.presentation.desktop.addpanel
 * Author: Lance
 * Date: Jun 24, 2012
 * Editor:
 */
public class BillingValidator extends AbstractValueModel implements Validator<ValidationResult>
{

    private static final long serialVersionUID = 1L;
    private String txtAddress;
    private String txtAmount;

    public BillingValidator()
    {
    }

    public BillingValidator(String txtAddress, String txtAmount)
    {
        this.txtAddress = txtAddress;
        this.txtAmount = txtAmount;
    }

    public String getTxtAddress()
    {
        return txtAddress;
    }

    public void setTxtAddress(String txtAddress)
    {
        this.txtAddress = txtAddress;
    }

    public String getTxtAmount()
    {
        return txtAmount;
    }

    public void setTxtAmount(String txtAmount)
    {
        this.txtAmount = txtAmount;
    }

    @Override
    public Object getValue()
    {
        return this;
    }

    @Override
    public void setValue(Object newValue)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValidationResult validate(ValidationResult validationTarget)
    {
        if (Strings.isEmpty(txtAmount.trim()))
        {
            validationTarget.addError("Billing amount required");
        }
        if (Strings.isEmpty(txtAddress.trim()))
        {
            validationTarget.addError("Billing address required");
        }
        try
        {
            Double.parseDouble(txtAmount.trim());
        }
        catch (Exception e)
        {
            validationTarget.addError("Billing amount must be numeric");
        }

        return validationTarget;
    }
}
